package logic

import (
	"context"
	"errors"
	"goods-manager/model"

	"goods-manager/goodrpc/goodrpc"
	"goods-manager/goodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodDetailLogic {
	return &GoodDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodDetailLogic) GoodDetail(in *goodrpc.GoodDetailRequest) (*goodrpc.GoodDetailResponse, error) {
	// todo: add your logic here and delete this line
	var good model.Good
	model.DB.Debug().Where("id=?", in.GoodID).Find(&good)
	if good.ID != 0 {
		return nil, errors.New("商品已存在,请更换")
	}

	return &goodrpc.GoodDetailResponse{
		GoodName:  good.GoodName,
		GoodPrice: float32(good.GoodPrice),
		GoodStock: int64(good.GoodStock),
		GoodStyle: good.GoodStyle,
	}, nil
}
